/**
 * 
 */
package com.ntv.presentation.spring.web.content;

import java.io.IOException;
import java.io.OutputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.ntv.service.DomainException;
import com.ntv.service.content.IContentAppService;

/**
 * @author Vedamoorthi Vairam
 *
 */
@Controller
public class ContentController {

	private final IContentAppService  contentAppService;

	@Autowired
	public ContentController(IContentAppService contentAppService) {
		this.contentAppService = contentAppService;
	}

	@RequestMapping("/imageContent.html")
	public void streamImageContent(@RequestParam("id") Long contentId, OutputStream outputStream) throws IOException {
		try {
			this.contentAppService.streamImage(contentId, outputStream);
		}
		catch (DomainException e) {
			
		}
	}

	@RequestMapping("/wishesImageContent.html")
	public void streamWishesImageContent(@RequestParam("id") Long id, OutputStream outputStream) throws IOException {
		try {
			this.contentAppService.streamWishesImage(id, outputStream);
		}
		catch (DomainException e) {
			
		}
	}
	
	@RequestMapping("/obituaryImageContent.html")
	public void streamObituaryImageContent(@RequestParam("id") Long id, OutputStream outputStream) throws IOException {
		try {
			this.contentAppService.streamObituaryImage(id, outputStream);
		}
		catch (DomainException e) {
			
		}
	}
	
	
	@RequestMapping("/imageUpload")
	public String processImageUpload(
			@RequestParam("name") String name, @RequestParam("description") String description,
			@RequestParam("image") MultipartFile image) throws IOException {

		try {
			this.contentAppService.storeImage(name, image.getInputStream(), (int) image.getSize(), description);
		}
		catch (DomainException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:imageList";
	}


}